import { mapGetters, mapState } from 'vuex'

export default {
    data() {
        return {
            orders: [],
            count: '-',
            pageNo: 0,
            pageSize: 15,
            refreshing: false,
            imgs: {
                circleOk: '/static/img/CIRCLE-OK@3x.png',
                circleDel: '/static/img/CIRCLE-DELETE@3x.png'
            }
        }
    },
    computed: {
        ...mapState(['user', 'order']),
        ...mapGetters(['orderReding', 'orderCouponing']),
        noMoreOrders() {
            if (this.count == '--') return false
            return this.orders.length >= this.count
        }
    },
    mounted() {
        $loading.show()
        $store.dispatch('userInfoAjax').then(res => {
            var { code, msg } = res.data
            if (code == 'S') {
                $loading.hide()
                // 手动设置样式
                var height
                if (this.user.borrowStatus == 'Y') {
                    height = window.innerHeight - this.$refs.latest.clientHeight
                    $store.dispatch('orderNewAjax')
                } else {
                    height = window.innerHeight
                }
                this.$refs.ordersScroller.style.height = height + 'px'
                this.$refs.records.$el.style.height = height - 40 + 'px'
                this.loadMoreOrders()
            } else {
                $toast.show(msg)
            }
        }).catch(err => {
            $loading.hide()
            $router.replace('/system/neterror')
        })
    },
    methods: {
        // 开关借款详细
        toogleDetail(i) {
            this.orders[i].open = !this.orders[i].open
        },
        // 加载更多订单
        loadMoreOrders() {
            if (!this.noMoreOrders || this.pageNo == 0) {
                this.pageNo++
                $ajax.order.list({
                    userId: $store.state.user.userId,
                    pageNo: this.pageNo,
                    pageSize: this.pageSize
                }).then(res => {
                    var { code, data } = res.data
                    if (code == 'S') {
                        for (var i = 0; i < data.list.length; i++) {
                            data.list[i].open = false
                        }
                        if (this.pageNo > 1) {
                            this.orders.push(...data.list)
                        } else {
                            this.orders = data.list
                        }
                        this.count = data.count
                    }
                    this.closeLoad()
                }).catch(err => {
                    this.closeLoad()
                    $loading.hide()
                    $router.replace('/system/neterror')
                })
            }
        },
        // 重新加载订单
        reLoadOrders() {
            this.pageNo = 0
            this.count = '-'
            this.refreshing = true
            this.loadMoreOrders()
        },
        // 关闭下拉刷新和上拉加载
        closeLoad() {
            this.refreshing = false
            this.$refs.records.infiniteDone()
            this.$refs.records.refreshDone()
        },
        // 查看协议
        examineProtocol(protocolUrl) {
            // https://imgfin.caiyoyoo.com//data/pdf/contract/20170523/1705230403273126.pdf
            $router.push({
                name: 'system_pdf',
                query: {
                    pdfUrl: protocolUrl
                }
            })
        }
    },
    mixins: [$mixins.needLogin]
}